package com.ultrasoft.farmasoft.web;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.SessionAttributes;

import com.ultrasoft.farmasoft.domain.Activesubstance;
import com.ultrasoft.farmasoft.domain.Composition;
import com.ultrasoft.farmasoft.domain.Item;
import com.ultrasoft.farmasoft.domain.Package;
import com.ultrasoft.farmasoft.domain.Pharmacyproduct;
import com.ultrasoft.farmasoft.domain.Product;
import com.ultrasoft.farmasoft.domain.Productattributes;
import com.ultrasoft.farmasoft.domain.Provider;
import com.ultrasoft.farmasoft.domain.Rol;
import com.ultrasoft.farmasoft.domain.Stock;
import com.ultrasoft.farmasoft.domain.Typecomposition;
import com.ultrasoft.farmasoft.domain.Userpharmacy;
import com.ultrasoft.farmasoft.service.ConfigServiceProducts;
import com.ultrasoft.farmasoft.service.ProviderService;
import com.ultrasoft.farmasoft.service.ReportsService;

@RestController
@SessionAttributes({"s_UserPharmacy"}) 
@RequestMapping("/service")
public class WebServicesController {
	private HttpSession session;
	private Userpharmacy s_UserPharmacy;
	private int s_userId;
	private int s_pharmacyId;

	private final ConfigServiceProducts configServiceProducts;
	@Autowired
	public WebServicesController(ConfigServiceProducts configServiceProducts){
		this.configServiceProducts	= configServiceProducts;
	}
	private boolean sessionControl(HttpServletRequest request){
		boolean res = false;
		session = request.getSession(true);
		if(session.getAttribute("s_UserPharmacy") != null){
			s_UserPharmacy 	= (Userpharmacy)session.getAttribute("s_UserPharmacy");
			s_userId 		= s_UserPharmacy.getUser().getUserId();
			s_pharmacyId 	= s_UserPharmacy.getPharmacy().getPharmacyId();
			res = true;
		}
		return res;
	}
	
	@RequestMapping(value = "/{name}", method = RequestMethod.GET)
	public String getGreeting(@PathVariable String name) {
		String result="Hello "+name;
		return result;
	}
	
	@RequestMapping(value = "/getMissing/{name}", method = RequestMethod.GET)
	public List<Rol> getMissingProduct(@PathVariable String name) {
		Rol rol1 = new Rol();
		rol1.setRolId(1);
		rol1.setName("Administrator");
		
		Rol rol2 = new Rol();
		rol2.setRolId(2);
		rol2.setName("User");
		
		Rol rol3 = new Rol();
		rol3.setRolId(3);
		rol3.setName("Counter");
		
		List<Rol> listRol = new ArrayList<Rol>();
		listRol.add(rol1);
		listRol.add(rol2);
		listRol.add(rol3);
		return listRol;
	}
	
	
	@RequestMapping(value = "/getExpired/{name}", method = RequestMethod.GET)
	public List<Product> getExpiredProduct(@PathVariable String name) {
		return getListProductTemp(configServiceProducts.getListProductByPharmacyIdComplete(s_pharmacyId,true));
	}	
	
	private ArrayList<Product> getListProductTemp(List<Product> listProductTemp){
		ArrayList<Product> listProduct = new ArrayList<Product>();
		for (Product product : listProductTemp) {			
			listProduct.add(getProductTemp(product));
		}
		return listProduct;
	}
	private Product getProductTemp(Product product){
		Product p = new Product(product.getProductPK().getProviderId(),product.getProductPK().getProductId());
		p.setName(product.getName());
		p.setPresentation(product.getPresentation());
		p.setActive(product.getActive());
		p.setGenericName(product.getGenericName());
		Activesubstance as = new Activesubstance(product.getActiveSubstanceId().getActiveSubstanceId(),product.getActiveSubstanceId().getSynonyms(),product.getActiveSubstanceId().getName(),product.getActiveSubstanceId().getTherapeuticAction(),product.getActiveSubstanceId().getProperties());
		p.setActiveSubstanceId(as);
		p.setCode(product.getCode());
		p.setCodeBar(product.getCodeBar());
		
		Provider provider = new Provider(product.getProvider().getProviderId(),product.getProvider().getName(),product.getProvider().getClient());
		if(product.getProvider().getAddress() != null)
			provider.setAddress(product.getProvider().getAddress());
		p.setProvider(provider);			
		
		List<Composition> listCompositionTemp = (List<Composition>)product.getCompositionCollection();
		List<Composition> listComposition = new ArrayList<Composition>();
		for (Composition c : listCompositionTemp) {
			Composition composition = new Composition(c.getCompositionId(),c.getName(),c.getQuantity(),c.getActive());
			Typecomposition typeComposition = new Typecomposition(c.getTypeCompositionId().getTypeCompositionId(),c.getTypeCompositionId().getName());
			composition.setTypeCompositionId(typeComposition);
			Product p1 = new Product(product.getProductPK().getProviderId(),product.getProductPK().getProductId());
			p1.setName(product.getName());
			p1.setPresentation(product.getPresentation());
			p1.setActive(product.getActive());
			p1.setGenericName(product.getGenericName());
			composition.setProduct(p1);
			listComposition.add(composition);
		}
		List<Productattributes> listProductAttributesTemp = (List<Productattributes>)product.getProductattributesCollection();
		List<Productattributes> listProductAttributes = new ArrayList<Productattributes>();
		for (Productattributes pa : listProductAttributesTemp) {
			Productattributes productAttributes = new Productattributes(pa.getProductattributesPK().getProductAttributeId(),pa.getProductattributesPK().getProviderId(),pa.getProductattributesPK().getProductId()); 
			listProductAttributes.add(productAttributes);
		}
		p.setCompositionCollection(listComposition);
		p.setProductattributesCollection(listProductAttributes);
		
		if(product.getPharmacyProduct() != null){
			Pharmacyproduct pp = new Pharmacyproduct(product.getPharmacyProduct().getPharmacyproductPK().getPharmacyId(),product.getPharmacyProduct().getPharmacyproductPK().getProviderId(),product.getPharmacyProduct().getPharmacyproductPK().getProductId()); 
			pp.setRepositionLevel(product.getPharmacyProduct().getRepositionLevel());
			pp.setPackage1(product.getPharmacyProduct().getPackage1());
			pp.setService(product.getPharmacyProduct().getService());
			pp.setRequiresPrescription(product.getPharmacyProduct().getRequiresPrescription());
			pp.setLocation(product.getPharmacyProduct().getLocation());
			pp.setActive(product.getPharmacyProduct().getActive());
			List<Package> listPackage = new ArrayList<Package>(); 
			List<Package> listPackageTemp = (List<Package>)product.getPharmacyProduct().getPackageCollection();	
			for (Package package1 : listPackageTemp) {
				List<Item> listItem = new ArrayList<Item>();
				List<Item> listItemTemp = (List<Item>)package1.getItemCollection();
				for (Item item : listItemTemp) {
					Item itemTemp = new Item(item.getItemPK().getPackageId(),item.getItemPK().getItemId());
					Stock s = new Stock(item.getStockId().getStockId(),item.getStockId().getName());
					itemTemp.setState(item.getState());
					itemTemp.setCost(item.getCost());
					itemTemp.setStockId(s);
					listItem.add(itemTemp);
				}
				Package pack = new Package(package1.getPackageId(),package1.getPackage1(),package1.getDateIncome(),package1.getDueDate());
				pack.setBatchNumber(package1.getBatchNumber());
				pack.setDueDate(package1.getDueDate());
				pack.setState(package1.getState());
				if(package1.getTempTotal1() != null)
					pack.setTempTotal1(package1.getTempTotal1());
				
				if(package1.getTempTotal2() != null)
					pack.setTempTotal2(package1.getTempTotal2());
				
				pack.setItemCollection(listItem);
				listPackage.add(pack);
			}	
			pp.setPackageCollection(listPackage);
			p.setPharmacyProduct(pp);
		}
		return p;
	}
}
